package com.travel.server.manager.impl.command.article;

import com.travel.server.manager.dto.request.article.QueryArticleDetailRequest;
import com.travel.server.manager.dto.response.article.QueryArticleDetailResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ArticleClassDal;
import com.travel.server.manager.impl.dal.ArticleDal;
import com.travel.server.manager.model.ArticleClassInfo;
import com.travel.server.manager.model.ArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:08
 * @Desc:
 */
@Component
public class QueryArticleDetailCommand extends AbstractCommand<QueryArticleDetailRequest, QueryArticleDetailResponse> {

    @Autowired
    private ArticleDal articleDal;

    @Autowired
    private ArticleClassDal articleClassDal;

    @Override
    protected void checkInput(QueryArticleDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryArticleDetailResponse handle(QueryArticleDetailRequest request) {
        checkInput(request);

        ArticleInfo articleInfo = articleDal.selectById(request.getId());
        ArticleClassInfo classInfo = articleClassDal.selectById(articleInfo.getClassId());

        QueryArticleDetailResponse response = new QueryArticleDetailResponse();
        response.setClassId(articleInfo.getClassId());
        response.setClassName(classInfo.getClassName());
        response.setContent(articleInfo.getContent());
        response.setIsPublish(articleInfo.getIsPublish());
        response.setTitle(articleInfo.getArticleTitle());

        return response;
    }
}
